﻿using System;
using System.Windows;
using System.Windows.Controls;
using jSLApp.Client.ApplicationBase;
using jSLApp.Client.MainService;
using jSLCore.Client.Presentation.BaseControl;
using jSLCore.Client.Presentation.BaseControl.DialogMessage;

namespace jSLApp.Client.Forms
{
    public partial class TopicDetail : DataControl
    {
        private readonly int _topicId;

        public TopicDetail(int topicId):this()
        {
            this._topicId = topicId;
        }

        public TopicDetail()
        {
            InitializeComponent();
        }

        private void gotoUser_Clicked(object sender, RoutedEventArgs e)
        {
            var hyperlinkButton = (HyperlinkButton)sender;
            MainContentContainer.Current.GotoUserAbout((string) hyperlinkButton.Tag);
        }

        private void copyLink_Clicked(object sender, RoutedEventArgs e)
        {
            var hyperlinkButton = (HyperlinkButton)sender;
            Clipboard.SetText((string) hyperlinkButton.Content);
        }

        public override void LoadInitialData()
        {
            DoWork("Загрузка топика",(sender, args) =>
                                         {
                                             args.Result = Communication.Proxy.GetTopic(args.Int);
                                         },(o, eventArgs) =>
                                                               {
                                                                   if(eventArgs.Error==null)
                                                                   {
                                                                       SetDataContext((Topic) eventArgs.Result);
                                                                   }
                                                               },_topicId);
        }

        public void SetDataContext(Topic topic)
        {
            Topic = topic;

            commentList.CommetableId = topic.id;
            subs.CommentableId = topic.id;
        }

        public Topic Topic
        {
            get { return (Topic) DataContext; }
            set { DataContext = value; }
        }

        private void deleteIt(object sender, RoutedEventArgs e)
        {
            DoWork("Удаление топика",(o, args) => Communication.Proxy.DeleteTopic(args.Int),(sender1, eventArgs) =>
                                                          {
                                                              if(eventArgs.Error==null)
                                                              {
                                                                  InfoBox.ShowDialog("Топик удалён");
                                                                  MainContentContainer.Current.GotoForum();
                                                              }
                                                          },Topic.id);
        }
    }
}
